import {
  seeItemShoppingCart,
  itemShoppingCart
} from "../../api/apis.js"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    color: '#ED204F',
    selectedAllStatus: false,
    total: 0,
    distribution: 10,
    hiddenmodalput: true,
    orderList: []
  },
  //选择物品价钱计算
  getTotalPrice() {
    let orderList = this.data.orderList;
    let total = 0;
    for (let i = 0; i < orderList.length; i++) {
      if (orderList[i].selected) {
        total += orderList[i].count * orderList[i].price
      }
    }
    this.setData({
      orderList: orderList,
      total: total
    });
  },
  //判断是否是全选
  judgmentAll: function () {
    let orderList = this.data.orderList
    let shoplen = orderList.length
    let lenIndex = 0;
    for (let i = 0; i < shoplen; i++) {
      orderList[i].selected && lenIndex++;
    }
    this.setData({
      selectedAllStatus: lenIndex == shoplen
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._initData()
  },
  _initData() {
    seeItemShoppingCart({
      data: {
        openid: app.globalData.openid
      },
      success: res => {
        if (res.state) {
          let carts = []
          res.data.forEach((item) => {
            let obj = {
              img: item.sku.img,
              title: item.name,
              // effect: '清爽滋养保湿补水滋润···',
              cpacity: item.sku.title,
              price: item.sku.firstPay,
              count: item.Count,
              selected: false,
              product_Id: item.product_Id,
              sku_id: item.sku.id,
              resell: item.resell
            }
            carts.push(obj)
          })
          this.setData({
            orderList: carts
          })
          this.getTotalPrice();
        }
      }
    })
  },
  //商品数量增加
  add: function (e) {
    console.log(e)
    const index = e.currentTarget.dataset.index;
    let orderList = this.data.orderList;
    let count = parseInt(orderList[index].count);
    count = count + 1;
    orderList[index].count = count;
    this.setData({
      orderList: orderList
    });
    this.getTotalPrice();
    this.changeCart(1, e.currentTarget.dataset.item.product_Id, e.currentTarget.dataset.item.sku_id)
  },
  //商品数量的减少
  reduce: function (e) {
    const index = e.currentTarget.dataset.index;
    let orderList = this.data.orderList;
    let count = orderList[index].count;
    if (count <= 1) {
      // return false;
      wx.showModal({
        title: '您确定删除吗？',
        content: '当前产品将会从购物车中消失哦！',
        confirmColor: "#E64340",
        success: res => {
          if (res.confirm) {
            this.data.orderList.splice(index, 1)
            this.setData({
              orderList: this.data.orderList
            })
            this.changeCart(-1, e.currentTarget.dataset.item.product_Id, e.currentTarget.dataset.item.sku_id)
          }
        }
      })
    } else {
      count = count - 1;
      orderList[index].count = count;
      this.changeCart(-1, e.currentTarget.dataset.item.product_Id, e.currentTarget.dataset.item.sku_id)
    }
    this.setData({
      orderList: orderList,
    });
    this.getTotalPrice();
  },
  //商品选择
  selectGood: function (e) {
    const index = e.currentTarget.dataset.index;
    let orderList = this.data.orderList;
    const selected = orderList[index].selected;
    let selectedAllStatus = this.data.selectedAllStatus
    selectedAllStatus = !selectedAllStatus;
    orderList[index].selected = !selected;
    this.getTotalPrice();
    this.judgmentAll()
  },
  //全选
  allSelect: function (e) {
    let selectedAllStatus = this.data.selectedAllStatus;
    console.log(selectedAllStatus)
    selectedAllStatus = !selectedAllStatus;
    let orderList = this.data.orderList;
    for (let i = 0; i < orderList.length; i++) {
      if (!orderList[i].resell) {
        orderList[i].selected = selectedAllStatus;
      }
    }
    this.setData({
      selectedAllStatus: selectedAllStatus,
      orderList: orderList
    });
    this.getTotalPrice();

  },
  changeCart(count, product_Id, sku_id) {
    wx.showLoading({
      title: count > 0 ? '添加中...' : '减少中...'
    })
    itemShoppingCart({
      data: {
        openid: app.globalData.openid,
        product_Id: product_Id,
        sku_id: sku_id,
        variable: count
      },
      success: res => {
        if (res.state) {
          wx.hideLoading()
        }
      }
    })
  },
  submit() {
    let confirmProduct = []
    this.data.orderList.forEach((item) => {
      if (item.selected) {
        confirmProduct.push(item)
      }
    })
    if (confirmProduct.length <= 0) {
      wx.showToast({
        title: '您还未选择任何商品！',
        icon: 'none'
      })
    } else {
      wx.setStorage({
        key: 'confirmProduct',
        data: confirmProduct,
        success: res => {
          wx.navigateTo({
            url: '../project-confirm/project-confirm',
          })
        }
      })
    }
  }
})